import sys
from typing import Any

import requests
import pip

from utils.location_util import getLocation
from utils.weatherJWT_util import getJWT

API_HOST = "ne5hv5j9wq.re.qweatherapi.com"


def getWeather(city : str) -> Any:
    url = f"https://{API_HOST}/v7/weather/3d"
    city_name, city_id = getLocation(city)
    if city_id == "" or city_name == "" :
        return "天气查询失败！！！！"
    params = {"location": f"{city_id}"}
    headers = {
        "Authorization": f"Bearer {getJWT()}",
        "Accept-Encoding": "gzip, deflate, br"  # 支持压缩
    }

    response = requests.get(url, params=params, headers=headers, timeout=30)

    # 检查请求是否成功
    if response.status_code == 200:
        # 响应会自动解压缩
        data = response.json()  # 如果返回的是JSON
        result = {}
        result["城市"] = city_name
        result["日期"] = data["daily"][0]["fxDate"]
        result["日出时间"] = data["daily"][0]["sunrise"]
        result["日落时间"] = data["daily"][0]["sunset"]
        result["月出时间"] = data["daily"][0]["moonrise"]
        result["月落时间"] = data["daily"][0]["moonset"]
        result["最高气温"] = data["daily"][0]["tempMax"]
        result["最低气温"] = data["daily"][0]["tempMin"]
        result["白天天气状况"] = data["daily"][0]["textDay"]
        result["夜间天气状况"] = data["daily"][0]["textNight"]
        result["白天风速"] = data["daily"][0]["windSpeedDay"]
        result["夜间风速"] = data["daily"][0]["windSpeedNight"]
        result["湿度"] = data["daily"][0]["humidity"]
        result["降水量"] = data["daily"][0]["precip"]
        result["云量"] = data["daily"][0]["cloud"]
        result["紫外线强度指数"] = data["daily"][0]["uvIndex"]
        return result
    else:
        print(f"请求失败，状态码: {response.status_code}")
        print(response.text)
        return "请求失败"


if __name__ == '__main__':
    print(getWeather("大连海事大学"))
